package megadrum.gui;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import megadrum.Common;

public class SimpleMidiLog extends Canvas {

    public SimpleMidiLog() {
        note = new int[Common.NUMBER_OF_SIMPLEMIDILOG_ENTRIES];
        padColor = new Color(51, 153, 255);
        chokeColor = new Color(255, 153, 0);
        setBackground(Color.WHITE);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        int width = getWidth();
        int height = getHeight();
        int barWidth = (width - 6 - note.length - note.length / 2) / note.length;
        int maxBarHeight = height - 6;

        g.setColor(Color.DARK_GRAY);
        g.drawRect(0, 0, width - 1, height - 1);

        int barHeight;
        for (int i = 0; i < note.length; i++) {
            if (note[i] == Common.CHOKE) {
                barHeight = maxBarHeight;
                g.setColor(chokeColor);
                g.fillRect(3 + i * (barWidth + 2), height - 3 - barHeight, barWidth, barHeight);
            } else {
                barHeight = (int) (maxBarHeight * ((double) note[i] / (double) 127));
                g.setColor(padColor);
                g.fillRect(3 + i * (barWidth + 2), height - 3 - barHeight, barWidth, barHeight);
            }
        }
    }

    public void insertEvent(int velocity) {
        for (int i = 0; i < note.length - 1; i++) {
            note[i] = note[i + 1];
        }
        note[note.length - 1] = velocity;
        repaint();
    }
    private int[] note;
    private Color padColor,  chokeColor;
}
